﻿using System;
using System.Windows;
using MyFloridaHouse.libSvOpenDistricts.StorageIsolated;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;

namespace MyFloridaHouse.libSvOpenDistricts.DataService
{
    public delegate void ServiceDALGetCensusGeometryCompletedEventHandler(CensusGeometrySTO sto);

    public partial class ServiceDAL
    {
        public static event ServiceDALGetCensusGeometryCompletedEventHandler GetCensusGeometryCompleted;

        public void GetCensusGeometry()
        {
            try
            {
				var proxy = new DistrBldrClient(_relativeBindingName);
                proxy.StartupGetCensusGeometryCompleted += OnServiceGetCensusGeometryCompleted;
                proxy.StartupGetCensusGeometryAsync(IsolatedStorageHelper.GetCensusGeometryFileDate());
                //proxy.StartupGetCensusGeometryAsync(IsolatedStorageHelper.GetCensusGeometryFileDate());

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void OnServiceGetCensusGeometryCompleted(object sender, StartupGetCensusGeometryCompletedEventArgs args)
        {
            if (!IsError("GetCensusGeometryCompleted", args.Error, args.Result, args.Result.errorMessage))
            {

                RaiseGetCensusGeometryCompleted(this, args.Result);
            }
        }

        private static void RaiseGetCensusGeometryCompleted(object sender, CensusGeometrySTO sto)
        {
            if (GetCensusGeometryCompleted != null)
            {
                GetCensusGeometryCompleted(sto);
            }
            else
            {
                // todo: Log this error
                throw (new NotImplementedException("No Subscribers to ServiceDALGetCensusGeometryCompleted Event"));
            }
        }

    }
}
